package com.ruoyi.core.util;

import com.ruoyi.common.utils.uuid.IdUtils;
import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

/**
 * 64为加密工具类
 * 
 * @author Darren Ho
 * 
 */
public class Base64Util {
	private static final String UTF_8 = "UTF-8";

	/**
	 * 对给定的字符串进行base64解码操作
	 */
	public static String decodeData(String inputData) {
		try {
			if (null == inputData) {
				return null;
			}
			return new String(Base64.decodeBase64(inputData.getBytes(UTF_8)), UTF_8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 对给定的字符串进行base64加密操作
	 */
	public static String encodeData(String inputData) {
		try {
			if (null == inputData) {
				return null;
			}
			return new String(Base64.encodeBase64(inputData.getBytes(UTF_8)),
                              UTF_8);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static void main(String[] args) {
		String x = IdUtils.simpleUUID();
		System.out.println(x);
		String encode = Base64Util.encodeData(x);
		System.out.println(encode);

		System.out.println(Base64Util.decodeData(encode));


	}

}
